package com.netease.nim.demo.session.viewholder;

import android.graphics.Color;
import android.text.method.LinkMovementMethod;
import android.text.style.ImageSpan;

import com.netease.nim.demo.session.emoji.MoonUtil;
import com.netease.nimlib.sdk.avchat.constant.AVChatRecordState;
import com.netease.nimlib.sdk.avchat.constant.AVChatType;
import com.netease.nimlib.sdk.avchat.model.AVChatAttachment;
import com.shwread.android.qysw10000204.R;

import java.util.Locale;


/**
 * Created by zhoujianghua on 2015/8/4.
 */
public class MsgViewHolderAVChatLeft
        extends
        MsgViewHolderTextLeft
{
	
	@Override
	protected void refresh(Object item) {
		super.refresh(item);
		textMessageTextView.setTextColor(Color.BLACK);
		AVChatAttachment avChatAttachment = (AVChatAttachment) messageItem.getMessage().getAttachment();
		textMessageTextView.setCompoundDrawablesWithIntrinsicBounds(avChatAttachment.getType() == AVChatType.AUDIO ? R.drawable.avchat_left_type_audio : R.drawable.avchat_left_type_video, 0, 0, 0);
		textMessageTextView.setCompoundDrawablePadding(8);
		MoonUtil.identifyFaceExpression(context, textMessageTextView, getDisplayText(), ImageSpan.ALIGN_BOTTOM);
		textMessageTextView.setMovementMethod(LinkMovementMethod.getInstance());
	}
	
	protected String getDisplayText() {
		AVChatAttachment avChatAttachment = (AVChatAttachment) messageItem.getMessage().getAttachment();
		if (avChatAttachment.getState().getValue() == AVChatRecordState.Missed.getValue()) return "未接听";
		if (avChatAttachment.getState().getValue() == AVChatRecordState.Rejected.getValue()) return "未接听";
		if (avChatAttachment.getState().getValue() == AVChatRecordState.Canceled.getValue()) return "通话取消";
		int hour = 0;
		if (avChatAttachment.getDuration() >= 60 * 60)
		{
			hour = avChatAttachment.getDuration() / (60 * 60);
		}
		int minute = avChatAttachment.getDuration() / 60;
		int second = avChatAttachment.getDuration() % 60;
		if (hour != 0) { return format(hour) + ":" + format(minute) + ":" + format(second); }
		return format(minute) + ":" + format(second);
	}
	
	private String format(int value) {
		return String.format(Locale.CHINA, "%02d", value);
	}
}
